package com.example.com.skills_utf8;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.Fragment;
import android.database.DataSetObserver;
import android.graphics.Point;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.CheckBox;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.TextView;

public class fragment32 extends Fragment{
	
	Activity activity ;
	
	private  List<String> groupArray;   
	private  List<List<String>> childArray;  
	
	Adapter adapter;
	
	Point cur_down_point = new Point();
	
	
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		
    	View v = inflater.inflate(R.layout.fragment_32, container, false);
        ExpandableListView listview = (ExpandableListView)v.findViewById(R.id.expandable_list_view);
    	
       
        
        groupArray = new  ArrayList<String>();   
        childArray = new  ArrayList<List<String>>();   
           
        groupArray.add("第一行" );   
        groupArray.add("第二行" );   
           
        List<String> tempArray = new  ArrayList<String>();   
        tempArray.add("第一条" );   
        tempArray.add("第二条" );   
        tempArray.add("第三条" );   
           
        for (int  index = 0 ; index <groupArray.size(); ++index)   
        {   
            childArray.add(tempArray);   
        }
        
        
        activity = getActivity();
		
		
		adapter = new Adapter();
		listview.setAdapter(adapter);
		
		
		
		// 可展开列表的点击
		listview.setOnChildClickListener( new OnChildClickListener() {
			
			@Override
			public boolean onChildClick(ExpandableListView parent, View v,
					int groupPosition, int childPosition, long id) {
				// TODO Auto-generated method stub
				
			//	System.out.println("setOnChildClickListener");
		
				
				if(true)
					return false;
			
				List<String>  bean =(List<String>)parent.getItemAtPosition(groupPosition);

				
				CheckBox cb_select = (CheckBox)v.findViewById(R.id.cb_select);
				
				
				Rect r = new Rect();
				cb_select.getLocalVisibleRect(r);
				System.out.println("getLocalVisibleRect: "+r.toString());
				
				
				int[] location1 = new int[2];
				cb_select.getLocationOnScreen(location1);
				System.out.println("getLocationOnScreen: "+location1[0]+" "+location1[1]);
				
				r.offset(location1[0], location1[1]);
				System.out.println("最终矩形数据: "+r.toString());
				System.out.println("最终点数据: "+cur_down_point.toString());
				
				if(r.contains(cur_down_point.x, cur_down_point.y)){
					//System.out.println("在目标区域.");
					
					cb_select.setChecked( !cb_select.isChecked() );
				}
				
				
				
				return false;
			}
		});
		
		listview.setOnGroupClickListener( new OnGroupClickListener() {
			
			@Override
			public boolean onGroupClick(ExpandableListView parent, View v,
					int groupPosition, long id) {
				// TODO Auto-generated method stub
				
				System.out.println("setOnGroupClickListener");
				
				
				Object obj = parent.getItemAtPosition(groupPosition);
				String bean = (String)obj;
				
		//		ArrayList<String>  bean =(ArrayList<String>)parent.getItemAtPosition(groupPosition);
				CheckBox cb_select = (CheckBox)v.findViewById(R.id.cb_select);
				
				
				Rect r = new Rect();
				cb_select.getLocalVisibleRect(r);
				System.out.println("getLocalVisibleRect: "+r.toString());
				
				
				int[] location1 = new int[2];
				cb_select.getLocationOnScreen(location1);
				System.out.println("getLocationOnScreen: "+location1[0]+" "+location1[1]);
				
				r.offset(location1[0], location1[1]);
				System.out.println("最终矩形数据: "+r.toString());
				System.out.println("最终点数据: "+cur_down_point.toString());
				
				if(r.contains(cur_down_point.x, cur_down_point.y)){
					System.out.println("在目标区域.");
					
					cb_select.setChecked( !cb_select.isChecked() );
					
					return true;
				}
				else{
					return false;
				}
				
				
			}
		});
		
		
		listview.setOnTouchListener( new OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				
				if(event.getAction() == MotionEvent.ACTION_DOWN){
					float x =event.getRawX();  // .
					float y =event.getRawY();
					System.out.println("X: "+x);  
					System.out.println("Y: "+y);  
					
					cur_down_point.x = (int)x;
					cur_down_point.y = (int)y;
					
					}
				
				return false;
			}
		});
		
		
		
		
		/*
		listview.setOnItemClickListener( new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
			
				System.out.println("setOnItemClickListener");
				
			}
		});
		*/
		
		
        return v;
		
		
	}

	
	
	
	
	
	// http://www.cnblogs.com/xuewater/archive/2012/08/09/2631017.html
	class Adapter implements ExpandableListAdapter
	{

		///*
		public  TextView getGenericView(String string)   
	    {   
	        // Layout parameters for the ExpandableListView    
	        AbsListView.LayoutParams layoutParams = new  AbsListView.LayoutParams(   
	                ViewGroup.LayoutParams.FILL_PARENT, 64 );   
	        TextView text = new  TextView(activity);   
	        text.setLayoutParams(layoutParams);   
	        // Center the text vertically    
	        text.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);   
	        // Set the text starting position    
	        text.setPadding(60 , 0 , 0 , 0 );   
	        text.setText(string);   
	        return  text;   
	    } 
		//*/
		
		@Override
		public void registerDataSetObserver(DataSetObserver observer) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void unregisterDataSetObserver(DataSetObserver observer) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public int getGroupCount() {
			// TODO Auto-generated method stub
			return  groupArray.size(); 
		}

		@Override
		public int getChildrenCount(int groupPosition) {
			// TODO Auto-generated method stub
			return  childArray.get(groupPosition).size();  
		}

		@Override
		public Object getGroup(int groupPosition) {
			// TODO Auto-generated method stub
			 return  groupArray.get(groupPosition); 
		}

		@Override
		public Object getChild(int groupPosition, int childPosition) {
			// TODO Auto-generated method stub
			return  childArray.get(groupPosition).get(childPosition);  
		}

		@Override
		public long getGroupId(int groupPosition) {
			// TODO Auto-generated method stub
			return groupPosition;
		}

		@Override
		public long getChildId(int groupPosition, int childPosition) {
			// TODO Auto-generated method stub
			 return  childPosition;  
		}

		@Override
		public boolean hasStableIds() {
			// TODO Auto-generated method stub
			return false;
		}

		@Override
		public View getGroupView(int groupPosition, boolean isExpanded,
				View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			
			final GroupViewHolder ViewHolder;
			
			if(convertView == null){
				convertView = activity.getLayoutInflater().inflate(R.layout.adapter_32_1, null);
				
				ViewHolder = new GroupViewHolder();
				ViewHolder.cb_select = (CheckBox)convertView.findViewById(R.id.cb_select);
				
				convertView.setTag(ViewHolder);
			}
			else{
				ViewHolder = (GroupViewHolder)convertView.getTag();
			}
			
			
			
			return convertView;
			
			
		//	String string = groupArray.get(groupPosition);   
	    //    return  getGenericView(string); 
		}

		@Override
		public View getChildView(int groupPosition, int childPosition,
				boolean isLastChild, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			
			String string = childArray.get(groupPosition).get(childPosition);   
	        return  getGenericView(string);
		}

		@Override
		public boolean isChildSelectable(int groupPosition, int childPosition) {
			// TODO Auto-generated method stub
			return true;
		}

		@Override
		public boolean areAllItemsEnabled() {
			// TODO Auto-generated method stub
			return false;
		}

		@Override
		public boolean isEmpty() {
			// TODO Auto-generated method stub
			return false;
		}

		@Override
		public void onGroupExpanded(int groupPosition) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onGroupCollapsed(int groupPosition) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public long getCombinedChildId(long groupId, long childId) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public long getCombinedGroupId(long groupId) {
			// TODO Auto-generated method stub
			return 0;
		}
		
	};
	
	
	class GroupViewHolder
	{
		public CheckBox cb_select;
	};
	
	
	class childViewHolder
	{
		
	};
	
	

}





